<?php 
/********
Author: Imman E. Riddle
Created On: 7/24/2012 2:33PM
Modified By: Imman Riddle
Modified On: 
Class Name: Admin
File Name: admin.php
********/

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {
	
	
	
	public function index()
	{
	
		if($this->session->userdata('logged_in') && ($this->session->userdata('role')=="Admin"))
		{
		$data['role'] = "Admin";
		}else{
			redirect('user');
		}
		
		$this->config->set_item('base_url', 'http://resmap.isoftsystemsinc.com/');
		
		//totals
		$total_votes = $this->db->query("SELECT COUNT(vote_mayor) AS total_mayor FROM votes");
		$us_votes = $this->db->query("SELECT COUNT(votes.vote_mayor) AS us_mayor FROM votes LEFT JOIN voters USING (vin) WHERE votes.vote_mayor='us'");
		$them_votes = $this->db->query("SELECT COUNT(votes.vote_mayor) AS them_mayor FROM votes LEFT JOIN voters USING (vin) WHERE  votes.vote_mayor='them'");
		$und_votes = $this->db->query("SELECT COUNT(votes.vote_mayor) AS und_mayor FROM votes LEFT JOIN voters USING (vin) WHERE votes.vote_mayor='und'");
		
		$data['total_mayor']  = $total_votes->row();
		$data['us_mayor']  = $us_votes->row();
		$data['them_mayor']  = $them_votes->row();
		$data['und_mayor']  = $und_votes->row();
		
		

	//by district	
	 	$this->db->select('COUNT(votes.vote_mayor) AS district1_total');
		$this->db->from('votes');
		$this->db->join('voters', 'voters.vin=votes.vin','left');
		$this->db->where('voters.district_code', '1');
		$this->db->limit('1'); 
		$district1_total = $this->db->get();  
		
	$district1_total = $this->db->query("SELECT COUNT(votes.vote_mayor) AS district1_total FROM votes LEFT JOIN voters USING (vin) WHERE voters.district_code='1'");
	$district2_total = $this->db->query("SELECT COUNT(votes.vote_mayor) AS district2_total FROM votes LEFT JOIN voters USING (vin) WHERE voters.district_code='2'");
        $this->db->select('COUNT(votes.vote_mayor) AS district2_total');
		$this->db->from('votes');
		$this->db->join('voters', 'voters.vin=votes.vin','left');
		$this->db->where('voters.district_code', '2');
		$this->db->limit('1'); 
		$district2_total = $this->db->get(); 
		
	$data['district1_total']  = $district1_total->row();
	$data['district2_total']  = $district2_total->row();
 
	
		

	

		$this->db->select('COUNT(votes.vote_mayor) AS district1_us');
		$this->db->from('votes');
		$this->db->join('voters', 'voters.vin=votes.vin','left');
		$this->db->where('voters.district_code', '1');
		$this->db->where('votes.vote_mayor', 'us');
		$this->db->limit('1'); 
		$district1_us = $this->db->get(); 
 	
	  $district1_us = $this->db->query("SELECT COUNT(votes.vote_mayor) AS district1_us FROM votes LEFT JOIN voters USING (vin) WHERE voters.district_code='1' AND votes.vote_mayor='us'");
	  $district2_us = $this->db->query("SELECT COUNT(votes.vote_mayor) AS district2_us FROM votes LEFT JOIN voters USING (vin) WHERE voters.district_code='2' AND votes.vote_mayor='us'");
	/* 	$this->db->select('COUNT(votes.vote_mayor) AS district2_us');
		$this->db->from('votes');
		$this->db->join('voters', 'voters.vin=votes.vin','left');
		$this->db->where('voters.district_code', '2');
		$this->db->where('votes.vote_mayor', 'us');
		$this->db->limit('1'); 
		$district2_us = $this->db->get(); 	
	 */	
	$data['district1_us'] =  $district1_us->row();
	$data['district2_us']  = $district2_us->row();
		
	/* 	$this->db->select('COUNT(votes.vote_mayor) AS district1_them');
		$this->db->from('votes');
		$this->db->join('voters', 'voters.vin=votes.vin','left');
		$this->db->where('voters.district_code', '1');
		$this->db->where('votes.vote_mayor', 'them');
		$this->db->limit('1'); 
		$district1_them = $this->db->get(); 
	 */	
	$district1_them = $this->db->query("SELECT COUNT(votes.vote_mayor) AS district1_them FROM votes LEFT JOIN voters USING (vin) WHERE voters.district_code='1' AND votes.vote_mayor='them'");
	$district2_them = $this->db->query("SELECT COUNT(votes.vote_mayor) AS district2_them FROM votes LEFT JOIN voters USING (vin) WHERE voters.district_code='2' AND votes.vote_mayor='them'");
	
	/* 	$this->db->select('COUNT(votes.vote_mayor) AS district2_them');
		$this->db->from('votes');
		$this->db->join('voters', 'voters.vin=votes.vin','left');
		$this->db->where('voters.district_code', '2');
		$this->db->where('votes.vote_mayor', 'them');
		$this->db->limit('1'); 
		$district2_them = $this->db->get(); 
	 */
	$data['district1_them'] =  $district1_them->row();
	$data['district2_them']  = $district2_them->row();

	/* 	$this->db->select('COUNT(votes.vote_mayor) AS district1_und');
		$this->db->from('votes');
		$this->db->join('voters', 'voters.vin=votes.vin','left');
		$this->db->where('voters.district_code', '1');
		$this->db->where('votes.vote_mayor', 'und');
		$this->db->limit('1'); 
		$district1_und = $this->db->get(); 
	 */
	$district1_und = $this->db->query("SELECT COUNT(votes.vote_mayor) AS district1_und FROM votes LEFT JOIN voters USING (vin) WHERE voters.district_code='1' AND votes.vote_mayor='und'");
	$district2_und = $this->db->query("SELECT COUNT(votes.vote_mayor) AS district2_und FROM votes LEFT JOIN voters USING (vin) WHERE voters.district_code='2' AND votes.vote_mayor='und'");
	
	/* 	$this->db->select('COUNT(votes.vote_mayor) AS district2_und');
		$this->db->from('votes');
		$this->db->join('voters', 'voters.vin=votes.vin','left');
		$this->db->where('voters.district_code', '2');
		$this->db->where('votes.vote_mayor', 'und');
		$this->db->limit('1'); 
		$district2_und = $this->db->get(); 
	 */	
	$data['district1_und'] =  $district1_und->row();
	$data['district2_und']  = $district2_und->row();
	
	
	$this->load->view('admin/admin_home', $data);	
	
	}
	
	public function precincts(){
$this->config->set_item('base_url', 'http://resmap.isoftsystemsinc.com/');
	//totals
		$total_votes = $this->db->query("SELECT COUNT(votes.vote_mayor) AS precinct_total FROM votes LEFT JOIN voters USING (vin) WHERE voters.district_code='" . $_GET['district'] . "' AND barangay_code='" . $_GET['brgy'] . "'");
		$us_votes = $this->db->query("SELECT COUNT(votes.vote_mayor) AS us_mayor FROM votes LEFT JOIN voters USING (vin) WHERE voters.district_code='" . $_GET['district'] . "' AND barangay_code='" . $_GET['brgy'] . "' AND votes.vote_mayor='us'");
		$them_votes = $this->db->query("SELECT COUNT(votes.vote_mayor) AS them_mayor FROM votes LEFT JOIN voters USING (vin) WHERE voters.district_code='" . $_GET['district'] . "' AND barangay_code='" . $_GET['brgy'] . "' AND votes.vote_mayor='them'");
		$und_votes = $this->db->query("SELECT COUNT(votes.vote_mayor) AS und_mayor FROM votes LEFT JOIN voters USING (vin) WHERE voters.district_code='" . $_GET['district'] . "' AND barangay_code='" . $_GET['brgy'] . "' AND votes.vote_mayor='und'");
		
		$data['precinct_total']  = $total_votes->row();
		$data['us_mayor']  = $us_votes->row();
		$data['them_mayor']  = $them_votes->row();
		$data['und_mayor']  = $und_votes->row();
	
	//precinct	
	
		$brgy_precinct = $this->db->query("SELECT DISTINCT(precinct_code) AS precinct FROM precinct LEFT JOIN voters USING (precinct_code) WHERE district_code='" . $_GET['district'] . "' AND barangay_code='" . $_GET['brgy'] . "'");	
		$data['brgy_precinct'] =   $brgy_precinct->result();
		
		$this->load->view('admin/admin_precinct', $data);
	
	
	}	
	


	public function barangay(){
$this->config->set_item('base_url', 'http://resmap.isoftsystemsinc.com/');
	
	//totals
		$total_votes = $this->db->query("SELECT COUNT(votes.vote_mayor) AS district_total FROM votes LEFT JOIN voters USING (vin) WHERE voters.district_code='" . $_GET['district'] . "'");
		$us_votes = $this->db->query("SELECT COUNT(votes.vote_mayor) AS us_mayor FROM votes LEFT JOIN voters USING (vin) WHERE voters.district_code='" . $_GET['district'] . "' AND votes.vote_mayor='us'");
		$them_votes = $this->db->query("SELECT COUNT(votes.vote_mayor) AS them_mayor FROM votes LEFT JOIN voters USING (vin) WHERE voters.district_code='" . $_GET['district'] . "' AND votes.vote_mayor='them'");
		$und_votes = $this->db->query("SELECT COUNT(votes.vote_mayor) AS und_mayor FROM votes LEFT JOIN voters USING (vin) WHERE voters.district_code='" . $_GET['district'] . "' AND votes.vote_mayor='und'");
		
		$data['district_total']  = $total_votes->row();
		$data['us_mayor']  = $us_votes->row();
		$data['them_mayor']  = $them_votes->row();
		$data['und_mayor']  = $und_votes->row();
	
	//barangay	
	
		$district_barangay = $this->db->query("SELECT DISTINCT(barangay_code) AS barangay FROM barangay LEFT JOIN voters USING (barangay_code) WHERE district_code='" . $_GET['district'] . "'");	
		$data['district_barangay'] =   $district_barangay->result();
		
		$this->load->view('admin/admin_barangay', $data);
	
	
	}	
	
	
	public function residents(){
$this->config->set_item('base_url', 'http://resmap.isoftsystemsinc.com/');
	
	//totals
		$total_votes = $this->db->query("SELECT COUNT(votes.vote_mayor) AS total FROM votes LEFT JOIN voters USING (vin) WHERE voters.district_code='" . $_GET['district'] . "' AND barangay_code='" . $_GET['brgy'] . "' AND precinct_code='" . $_GET['precinct'] . "'");
		
		$us_votes = $this->db->query("SELECT COUNT(votes.vote_mayor) AS us FROM votes LEFT JOIN voters USING (vin) WHERE voters.district_code='" . $_GET['district'] . "' 
		AND barangay_code='" . $_GET['brgy'] . "'  AND precinct_code='" . $_GET['precinct'] . "' AND votes.vote_mayor='us'");
		
		$them_votes = $this->db->query("SELECT COUNT(votes.vote_mayor) AS them FROM votes LEFT JOIN voters USING (vin) WHERE voters.district_code='" . $_GET['district'] . "' AND barangay_code='" . $_GET['brgy'] . "'  AND precinct_code='" . $_GET['precinct'] . "' AND votes.vote_mayor='them'");
		
		$und_votes = $this->db->query("SELECT COUNT(votes.vote_mayor) AS und FROM votes LEFT JOIN voters USING (vin) WHERE voters.district_code='" . $_GET['district'] . "' AND barangay_code='" . $_GET['brgy'] . "'  AND precinct_code='" . $_GET['precinct'] . "' AND votes.vote_mayor='und'");
		
		$data['total']  = $total_votes->row();
		$data['us']  = $us_votes->row();
		$data['them']  = $them_votes->row();
		$data['und']  = $und_votes->row();
	
	//precinct	
	
		$residents = $this->db->query("SELECT vote_mayor,vin,lastname, firstname, middlename FROM voters LEFT JOIN votes USING (vin) WHERE district_code='" . $_GET['district'] . "' AND barangay_code='" . $_GET['brgy'] . "' AND precinct_code='" . $_GET['precinct'] . "' ORDER BY lastname");	
		$data['residents'] =   $residents->result();
		
		$this->load->view('admin/admin_residents', $data);
	
	
	}
	public function vote()
	{
		$this->load->model('EditData');
		$this->EditData->update_vote(); 
	}
	


}

/* End of file admin.php */
/* Location: ./application/controllers/admin.php */